package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.service.impl.MenuServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.beancontext.BeanContextServiceRevokedListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/menu/menus")
public class MenuServlet extends HttpServlet {
    MenuServiceImpl menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("get".equals(method)){
            //全部查询
            getMenu(req,resp);

        }else if("add".equals(method)){
            //添加信息
            addMenu(req,resp);

        }else if("delete".equals(method)){
            //删除信息
            deleteMenu(req,resp);

        }else if("update".equals(method)){
            //修改信息
            updateMenu(req,resp);

        }else if("menuAdd".equals(method)){
            //跳转add页面   获取顶级级别菜单
            menuAdd(req, resp);

        }else if("menuInfo".equals(method)){
            //添砖info页面  获取
            menuInfo(req,resp);

        }else if("menuEdit".equals(method)){
            //跳转修改页面
            menuEdit(req,resp);

        }
    }

    /*protected void GetRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }*/

    //跳转info页面
    protected void menuInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Menu byId = menuService.findById(Integer.parseInt(id));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        req.setAttribute("byId",byId);
        req.getRequestDispatcher("info.jsp").forward(req,resp);

    }

    //修改信息
    protected void updateMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        System.out.println(id);
        String menuName = req.getParameter("menuName");
        String upMenuId = req.getParameter("upMenuId");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String inDesc = req.getParameter("inDesc");

        Menu menu = new Menu();
        menu.setMenuId(Integer.parseInt(id));
        menu.setMenuName(menuName);
        menu.setUpMenuId(Integer.parseInt(upMenuId));
        menu.setUrl(url);
        menu.setState(Integer.parseInt(state));
        menu.setInDesc(inDesc);

        int i = menuService.updateMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/power/menu/menus?method=get';</script>");
        }else {
            writer.println("<script>alert('修改失败');location.href='/power//menu/menus?method=get';</script>");
        }


    }

    //跳转edit页面
    protected void menuEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Menu byId = menuService.findById(Integer.parseInt(id));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        req.setAttribute("byId",byId);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //删除信息
    protected void deleteMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        int i = menuService.deleteMenu(Integer.parseInt(id));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/menu/menus?method=get';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power//menu/menus?method=get';</script>");
        }



    }

    //添加数据
    protected void addMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据
        String menuName = req.getParameter("menuName");
        String upMenuId = req.getParameter("upMenuId");
        String url = req.getParameter("url");
        String state = req.getParameter("state");
        String inDesc = req.getParameter("inDesc");

        //把数据存入对象
        Menu menu = new Menu();
        menu.setMenuName(menuName);
        menu.setUpMenuId(Integer.parseInt(upMenuId));
        menu.setUrl(url);
        menu.setState(Integer.parseInt(state));
        menu.setInDesc(inDesc);


        //调用service层

        int i = menuService.addMenu(menu);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('添加成功');location.href='/power/menu/menus?method=get';</script>");
        }else {
            writer.println("<script>alert('添加失败');location.href='/power//menu/menus?method=get';</script>");
        }


    }

    //跳转到add页面
    protected void menuAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }


    //查看全部  分页查询
    protected void getMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        PageUtil pageUtil = new PageUtil();
        List<Menu> menu = menuService.getMenu(index,pageUtil.getPageSize());
        int total = menuService.total();
        pageUtil.setPageIndex(index);
        pageUtil.setDataList(menu);
        pageUtil.setTotal(total);
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }

}
